package com.service.impl;

import com.mapper.UserMessageMapper;
import com.pojo.UserMessage;
import com.pojo.UserMessageExample;
import com.service.UserMessageService;
import com.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: HL
 * Date: 2020/3/3
 * Time: 14:34
 * Description: No Description
 */
@Service
public class UserMessageServiceImpl implements UserMessageService {
    @Autowired
    private UserMessageMapper userMessageMapper;

    @Override
    public ServerResponse save(UserMessage userMessage) {
        Byte status =1;
        userMessage.setStatus(status);
        //执行mybatis添加方法
        int rows = userMessageMapper.insert(userMessage);
        //判断添加成功
        if (rows > 0)
            return ServerResponse.addSuccess();
        else
            return ServerResponse.addFailed();
    }

    @Override
    public ServerResponse modifyStatus(Integer id) {
        UserMessage userMessage = userMessageMapper.selectByPrimaryKey(id);
        Byte status = 0;
        userMessage.setStatus(status);
        int i = userMessageMapper.updateByPrimaryKeySelective(userMessage);
        if (i>0)
            return ServerResponse.deleteSuccess();
        return ServerResponse.deleteFailed();
    }

    @Override
    public ServerResponse modify(UserMessage userMessage) {
        int i = userMessageMapper.updateByPrimaryKeySelective(userMessage);
        if (i>0)
            return ServerResponse.updateSuccess();
        return ServerResponse.updateFailed();
    }

    @Override
    public ServerResponse getById(Integer id) {
        UserMessage userMessage = userMessageMapper.selectByPrimaryKey(id);
        return ServerResponse.getDataSuccess(userMessage);
    }

    @Override
    public ServerResponse getAll() {
        UserMessageExample example=new UserMessageExample();
        UserMessageExample.Criteria criteria = example.createCriteria();
        Byte status = 1;
        criteria.andStatusEqualTo(status);
        List<UserMessage> userMessages = userMessageMapper.selectByExample(example);
        if (userMessages!=null&&userMessages.size()>0)
            return ServerResponse.getDataSuccess(userMessages);
        return ServerResponse.getDataFailed("没有数据");
    }
}
